package com.biz.primus.ms.homepage.helper;

import com.biz.primus.homepage.vo.CoverTemplateData;
import com.biz.primus.homepage.vo.MethodExecutorMethodParam;

import java.lang.reflect.Method;


/**
 * Created by defei on 2/10/17.
 */
public class MethodExecutor {

    public static final String EXECUTE_METHOD_NAME = "execute";

    private Object bean;

    private Method method;

    private MethodExecutorMethodParam param;

    public MethodExecutor(String bean, MethodExecutorMethodParam executorMethodParam) {
        if (bean== null){
            throw new IllegalArgumentException("Argument bean is null.");
        }
        MethodExecutable beanEntity = BeanDiscoverer.getBean(bean);
        try {
            Class<?> beanClass = beanEntity.getClass();
            method = beanClass.getMethod(EXECUTE_METHOD_NAME, MethodExecutorMethodParam.class);
        } catch (Throwable e) {
            throw new RuntimeException("Method not found.", e);
        }
        this.bean = beanEntity;
        this.param = executorMethodParam;
    }

    public CoverTemplateData execute() {

        try {
            return (CoverTemplateData) method.invoke(bean, param);
        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
    }
}
